import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        while(T-- > 0) {
            int n = in.nextInt();
            int k = in.nextInt();
            in.nextLine();
            String s = in.nextLine();
            int count = 0;//连胜次数
            int ret = 0;
            for(int i = 0; i < n; i++) {
                char ch = s.charAt(i);
                if(ch == 'W') {
                    count++;
                    if(count >= 3) {
                        ret += k;
                    }else {
                        ret += 1;
                    }
                }else {
                    count = 0;
                    ret -= 1;
                }
            }
            System.out.println(ret);
        }
    }
}